<?php

namespace app\goods\controller;

use app\common\controller\Adminbase;
use app\model\Shop as ShopModel;
use app\model\Goods as GoodsModel;

class Shop extends Adminbase
{
    public function index()
    {
        $model = 'Shop';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list   = ShopModel::where($map)
                ->field('*')
                ->with(['category'])
                ->page($page, $pageSize)
                ->order('sort', 'asc')
                ->order('id', 'asc')
                ->select();
            $total  = ShopModel::where($map)->count();
            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setDataUrl()
            ->setFormUrl('form')
            ->setLimit()
            ->setModel($model)
            ->addTableCheckBox()
            ->setSwitchTpl('switchTpl', 'status')
            ->setSwitchTpl('amplifyTpl', 'image')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('shop_appid', '小店id')
            ->addTableColumn('name', '名称')
            ->addTableColumn('image', '图片(自动拉取数据图片在前端正常展示)', null, '#amplifyTpl')
            ->addTableColumn('category_name', '分类')
            ->addTableColumn('sort', '排序', null, null, false, '', false, '', true)
            ->addTableColumn('flags', '标识', 220)
            ->addTableColumn('sale_num', '已售数量')
            ->addTableColumn('bug_num', '已购数量')
            ->addTableColumn('delivery_time', '发货速度')
            ->addTableOperation(220)
            ->setListLayerBtn([
                ['title' => '选品管理', 'url' => url('goods'), 'area' => "['95vw', '90vh']", 'class' => 'layui-btn-normal'],
            ])
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

    public function form()
    {
        $model = 'Shop';

        $id = $this->request->param('id/d');
        if (isset($id) && $id > 0) {
            $info = ShopModel::get($id);
            $this->assign('id', $id);
            $this->assign('action', 2);
        } else {
            $info = [
                'sort' => 50,
                'type' => 1,
            ];
        }

        $category = getModelList('ShopCategory', 'id, name', 'name', ['status' => 1]);
        $builder  = new \app\common\controller\FormBuilder();
        $builder
            ->setModel($model)
            ->setSuccessUrl()
            ->addFormItem('name', 'text', '名称', '', [], 1)
            ->addFormItem('shop_appid', 'text', '小店id', '', [])
            ->addFormItem('image', 'image', '图片', '', [])
            ->addFormItem('category_id', 'select', '分类', '', $category)
            ->addFormItem('flags', 'tags', '标识', '', [])
            ->addFormItem('sale_num', 'text', '已售数量', '', [])
            ->addFormItem('bug_num', 'text', '已购数量', '', [])
            ->addFormItem('delivery_time', 'text', '发货速度', '', [])
            ->addFormItem('sort', 'gtnumber', '排序', '值越小排序越靠前')
            ->setFormData($info)
            ->returnForm();
        $this->assign('module', 'app');
        $this->assign('is_verify', 0);
        return $this->fetch('admin@common/form');
    }

    public function goods()
    {
        $id         = $this->request->param('id/d');
        $shop_appid = GoodsModel::where('id', $id)->value('shop_appid');
        $model      = 'Goods';

        if ($this->request->isAjax()) {
            $map      = $this->buildParamsMap();
            $page     = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : 20;

            $list   = GoodsModel::where($map)
                ->where('shop_appid', $shop_appid)
                ->withAttr('source', function ($value, $data) {
                    $text = [1 => '定向', 2 => '公开'];
                    return $value ? $text[$value] : '';
                })
                ->withAttr('merchant_type', function ($value) {
                    $text = [1 => '入驻商家', 2 => '广场商家'];
                    return $value ? $text[$value] : '';
                })
                ->withAttr('is_square', function ($value) {
                    $text = [1 => '是', 2 => '否'];
                    return $value ? $text[$value] : '';
                })
                ->withAttr('status', function ($value) {
                    $text = [1 => '已上架', 2 => '已下架', 3 => '待选品', 5 => '已清退'];
                    return $value ? $text[$value] : '';
                })
                ->withAttr('commission', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : 0;
                })
                ->withAttr('share_commission', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : 0;
                })
                ->withAttr('subsidy_commission', function ($value) {
                    return $value > 0 ? number_format($value / 100, 2) : 0;
                })
                ->field('*')
                ->page($page, $pageSize)
                ->order('sort', 'asc')
                ->order('id', 'desc')
                ->select();
            $total  = GoodsModel::where($map)->where('shop_appid', $shop_appid)->count();
            $result = ["code" => 0, "count" => $total, "data" => $list];
            return json($result);
        }

        //生成列表数据
        $builder = new \app\common\controller\ListBuilder();
        $builder
            ->setAddBtn(false)
            ->setDelAllBtn(false)
            ->setDataUrl('goods', ['id' => $id])
            ->setLimit()
            ->setModel($model)
            ->setSwitchTpl('switchTpl', 'status')
            ->setSwitchTpl('amplifyTpl', 'image')
            ->addTableColumn('id', 'ID', null, null, true)
            ->addTableColumn('sort', '排序', null, null, false, '', false, '', true)
            ->addTableColumn('product_id', '商品ID', 180)
            ->addTableColumn('title', '标题', 550)
            ->addTableColumn('image', '图片', null, '#amplifyTpl')
            ->addTableColumn('stock_num', '库存', 120)
            ->addTableColumn('sale_price', '销售价格', 150)
            ->addTableColumn('commission_rate', '佣金率（%）', 150)
            ->addTableColumn('commission', '佣金', 150)
            ->addTableColumn('share_commission', '分享赚佣金', 150)
            ->addTableColumn('subsidy_commission', '补贴佣金', 150)
            ->addTableColumn('status', '状态', 168)
            ->addTableColumn('source', '来源', 120)
            ->addTableColumn('merchant_type', '商家类型', 150)
            ->addTableColumn('is_square', '是否允许上广场', 130)
            ->returnForm();

        $this->assign('module', 'app');
        return $this->fetch('admin@common/lists');
    }

}
